<?php
namespace app\shop\controller;
use app\common\controller\Common;
use app\shop\validate\GoodsValidate;
use app\common\model\Category;
use app\shop\model\GoodsModel;
use app\common\model\Goods as GoodsLis;
use app\common\model\Supplier;
use app\common\model\Stock;
use app\common\model\News;
use think\Exception;
use think\Request;
use think\Db;

/**
* 商品管理
*/
class Goods extends Common
{
	/**
	* 添加商品信息
	*/
	public function addgoods()
	{
		if(request()->isPost())
		{
			try{
				$info = Request::instance()->param();
				/*参数验证*/
				 $GoodsValidate = new GoodsValidate();
				 if(!$GoodsValidate->scene('addgoods')->check($info))
				 {
					return json(back(config('status.STATUS_CODE_SIX'),$GoodsValidate->getError()));
				 }
				 
				return json(GoodsModel::setGoods($info));
			}catch (\Exception $e){
				return json(back(config('state.STATUS_CODE_FOURONE'),$e->getMessage()));
			}
		}

		$this->assign('fcat',(new Category())->categoryList(['fid'=>0],'category_id,category_name'));
		$this->assign('news',(new News())->newsList(['type'=>1]));
		return $this->fetch();	
	}
	/**
	* 修改商品信息
	*/
	public function editgoods()
	{
		$info = Request::instance()->param();
		if(request()->isPost())
		{
			try{
				/*参数验证*/
				 /* $GoodsValidate = new GoodsValidate();
				 if(!$GoodsValidate->scene('addgoods')->check($info))
				 {
					return json(back(config('status.STATUS_CODE_SIX'),$GoodsValidate->getError()));
				 } */
				 
				return json(GoodsModel::editGoods($info));
			}catch (\Exception $e){
				return json(back(config('state.STATUS_CODE_FOURONE'),$e->getMessage()));
			}
		}
		
		$this->assign('goods',GoodsModel::goodsInfo($info['id']));
		$this->assign('fcat',(new Category())->categoryList(['fid'=>0],'category_id,category_name'));
		return $this->fetch();	
	}
	/**
	* 商品列表信息
	*/
	public function goodslist()
	{
		if(request()->isPost())
		{
			$info = Request::instance()->param();
			return GoodsModel::getGoodsList($info);
		}
		$this->assign('fcat',(new Category())->categoryList(['fid'=>0],'category_id,category_name'));
		return $this->fetch();
	}
	/**
	* 删除
	*/
	public function delgoods()
	{
		if(request()->isPost())
		{
			try{
				$info = Request::instance()->param();
				return json(GoodsModel::delGoods($info['id']));
			}catch (\Exception $e){
				return json(back(config('state.STATUS_CODE_FOURONE'),$e->getMessage()));
			}
		}
	}
	/**
	* 产品管理
	*/
	public function goods()
	{
		if(request()->isPost())
		{
			$info = Request::instance()->param();
			return GoodsModel::getGoodsList($info);
		}
		return $this->fetch();
	}
	/************库存**************/
	/**
	* 产品入库
	*/
	public function addstock()
	{
		$info = Request::instance()->param();
		if(request()->isPost())
		{
			try{
				return json(GoodsModel::setStock($info));
			}catch (\Exception $e){
				return json(back(config('state.STATUS_CODE_FOURONE'),$e->getMessage()));
			}
		}
		
		$this->assign('supp',(new Supplier())->supplierList(['is_show'=>0],'supplier_id,supplier_name'));
		$this->assign('goods',GoodsModel::goodsInfo($info['id'],true));
		return $this->fetch();	
	}
	/**
	* 产品库存信息
	*/
	public function storage()
	{
		$info = Request::instance()->param();
		if(request()->isPost())
		{
			return GoodsModel::stockList($info);
		}
		$this->assign('goods',GoodsModel::goodsInfo($info['id'],true));
		return $this->fetch();
	}
	/**
	* 删除库存信息
	*/
	public function delstorage()
	{
		if(request()->isPost())
		{
			try{
				$info = Request::instance()->param();
				return json(GoodsModel::delStorage($info['id']));
			}catch (\Exception $e){
				return json(back(config('state.STATUS_CODE_FOURONE'),$e->getMessage()));
			}
		}
	}
	/**
	* 编辑
	*/
	public function editstorage()
	{
		$info = Request::instance()->param();
		if(request()->isPost())
		{
			try{
				return json(GoodsModel::editStorage($info));
			}catch (\Exception $e){
				return json(back(config('state.STATUS_CODE_FOURONE'),$e->getMessage()));
			}
		}
		
		$this->assign('stock',GoodsModel::stockInfo($info['id']));
		$this->assign('supp',(new Supplier())->supplierList(['is_show'=>0],'supplier_id,supplier_name'));
		return $this->fetch();	
	}
	/**
	* 库存报损
	*/
	public function stockloss()
	{
		$info = Request::instance()->param();
		if(request()->isPost())
		{
			try{
				return json(GoodsModel::setStockloss($info));
			}catch (\Exception $e){
				return json(back(config('state.STATUS_CODE_FOURONE'),$e->getMessage()));
			}
		}
		$this->assign('goods',GoodsModel::goodsInfo($info['gid'],true));
		$this->assign('stock',GoodsModel::stockInfo($info['id']));
		$this->assign('news',(new News())->newsList(['type'=>2]));
		return $this->fetch();
	}
	/**
	* 返回总部记录
	*/
	public function returnstocklog()
	{
		if(request()->isPost())
		{
			$info = Request::instance()->param();
            $field = 's.stock_id,g.goods_id,g.goods_name,g.unit,s.news_name,s.number,s.imei,s.sn,s.period_time,s.buying_price,s.contract_sn,s.supplier_id,s.position,s.period_time,s.addtime';
			return GoodsModel::stockLossList($info,['s.state'=>3],$field);
		}
		$this->assign('fcat',(new Category())->categoryList(['fid'=>0],'category_id,category_name'));
		return $this->fetch();
	}
	/**
	* 报损库存
	*/
	public function stocklosslist()
	{
		if(request()->isPost())
		{
			$info = Request::instance()->param();
            $field = 's.stock_id,g.goods_id,g.goods_name,g.unit,s.news_name,s.number,s.imei,s.sn,s.period_time,s.buying_price,s.contract_sn,s.supplier_id,s.position,s.period_time,s.addtime';
			return GoodsModel::stockLossList($info,['s.state'=>2],$field);
		}
		$this->assign('fcat',(new Category())->categoryList(['fid'=>0],'category_id,category_name'));
		return $this->fetch();
	}
	/**
	* 恢复库存
	*/
	public function recovery()
	{
		if(request()->isPost())
		{
			try{
				$info = Request::instance()->param();
				return GoodsModel::recoveryStock($info);
			}catch (\Exception $e){
				return json(back(config('state.STATUS_CODE_FOURONE'),$e->getMessage()));
			}
		}
	}
	/**
	* 返回总部
	*/
	public function returnstock()
	{
		if(request()->isPost())
		{
			try{
				$info = Request::instance()->param();
				return GoodsModel::returnStock($info);
			}catch (\Exception $e){
				return json(back(config('state.STATUS_CODE_FOURONE'),$e->getMessage()));
			}
		}
	}
	/*************批量改价***************/
	/**
	* 批量改价
	*/
	public function amends()
	{
		$info = Request::instance()->param();
		if(request()->isPost())
		{
			try{
				/*开启事物*/
				Db::startTrans();
				$GoodsLis = new GoodsLis();
				foreach($info['goods_id'] as $i=>$id)
				{
					$data = ['selling_price'=>$info['selling_price'][$i],'trade_price'=>$info['trade_price'][$i]];
					if($GoodsLis->updateGoods(['goods_id'=>$id],$data) === false)
					{
						Db::rollback();
						return back(config('state.STATUS_CODE_TWO'),config('state.STATUS_MSG_TWO')); 
					}
				}
				Db::commit();
				return back(config('state.STATUS_CODE_ONE'),config('state.STATUS_MSG_ONE'));
			}catch (\Exception $e){
				return json(back(config('state.STATUS_CODE_FOURONE'),$e->getMessage()));
			}
		}
		$goodslist = (new GoodsLis())->goodsList(['goods_id'=>['in',$info['ids']]],'goods_id,goods_name,selling_price,trade_price');
		$this->assign('goodslist',$goodslist);
		return $this->fetch();
	}
    /*************过期产品***************/
    /**
    * 根据保质期查找到过期记录
    */
    public function overduegoods()
    {
        if(request()->isPost())
		{
			$info = Request::instance()->param();
            $field = 's.stock_id,g.goods_id,g.goods_name,g.unit,g.selling_price,g.trade_price,s.buying_price,s.imei,s.sn,s.period_time,s.number,s.position,s.remarks,s.supplier_id,s.contract_sn';
			return GoodsModel::stockLossList($info,['s.period_time'=>['<',strtotime(date('Y-m-d',time()))]],$field,true);
		}
		$this->assign('fcat',(new Category())->categoryList(['fid'=>0],'category_id,category_name'));
		return $this->fetch();
    }
    /**
    * 编辑保质期
    */
    public function editoverdue()
    {
        $info = Request::instance()->param();
		if(request()->isPost())
		{
			try{
				return json(GoodsModel::editOverdue($info));
			}catch (\Exception $e){
				return json(back(config('state.STATUS_CODE_FOURONE'),$e->getMessage()));
			}
		}
		$stock = GoodsModel::stockInfo($info['id'],true);
		$this->assign('stock',$stock);
		$this->assign('goods',GoodsModel::goodsInfo($stock['goods_id'],true));
		return $this->fetch();	
    }
    /*************退货、召回产品***************/
    /**
	* 退货、召回
	*/
    public function stockafter()
    {
        if(request()->isPost())
		{
			$info = Request::instance()->param();
            $field = 's.stock_id,g.goods_id,g.goods_name,g.category_id,g.cat_id,g.brand_name,g.memory_name,g.attribute_name,g.colour_name,s.position,s.remarks,s.supplier_id,s.contract_sn,s.period_time,s.state';
			return GoodsModel::stockLossList($info,['s.state'=>['IN','3,4']],$field,true);
		}
        $this->assign('supp',(new Supplier())->supplierList(['is_show'=>0],'supplier_id,supplier_name'));
        $this->assign('fcat',(new Category())->categoryList(['fid'=>0],'category_id,category_name'));
        return $this->fetch();
    }
    /*************产品历史记录**************/
    public function historygoods()
    {
        if(request()->isPost())
		{
			$info = Request::instance()->param();
			return GoodsModel::historyGoods($info);
		}
        $this->assign('fcat',(new Category())->categoryList(['fid'=>0],'category_id,category_name'));
        return $this->fetch();
    }
}
